package com.kim.store.service.impl;

import com.kim.store.entity.Address;
import com.kim.store.entity.Order;
import com.kim.store.entity.OrderItem;
import com.kim.store.entity.Product;
import com.kim.store.mapper.OrderMapper;
import com.kim.store.service.IAddressService;
import com.kim.store.service.ICartService;
import com.kim.store.service.IOrderService;
import com.kim.store.service.IProductService;
import com.kim.store.service.ex.UpdateException;
import com.kim.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private IAddressService addressService;
    @Autowired
    private ICartService cartService;
    @Autowired
    private IProductService productService;

    @Override
    public Order create(Integer aid, Integer uid, String username, Integer[] cid) {
        //即将下单的列表
        List<CartVO> list = cartService.getVOByCid(cid, uid);
        //计算商品的总价
        Long totalPrice = 0L;
        for (CartVO cartVO : list) {
            totalPrice += cartVO.getNum() * cartVO.getRealPrice();
        }
        //System.err.println(totalPrice);
        Address address = addressService.getByAid(aid, uid);
        //补全数据
        Order order = new Order();
        order.setUid(uid);
        //收货地址数据
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        //补全支付、总价、时间
        order.setStatus(0);
        order.setTotalPrice(totalPrice);
        order.setOrderTime(new Date());
        //补全日志
        order.setCreatedTime(new Date());
        order.setModifiedTime(new Date());
        order.setCreatedUser(username);
        order.setModifiedUser(username);
        //创建订单数据
        Integer row = orderMapper.insertOrder(order);
        if(row != 1){
            throw new UpdateException("插入数据异常");
        }
        //创建订单详细项数据
        for (CartVO cartVO : list) {
            OrderItem orderItem = new OrderItem();
            //补全数据
            orderItem.setOid(order.getOid());
            orderItem.setPid(cartVO.getPid());
            orderItem.setTitle(cartVO.getTitle());
            orderItem.setImage(cartVO.getImage());
            orderItem.setPrice(cartVO.getRealPrice());
            orderItem.setNum(cartVO.getNum());
            //补全日志
            orderItem.setCreatedTime(new Date());
            orderItem.setModifiedTime(new Date());
            orderItem.setCreatedUser(username);
            orderItem.setModifiedUser(username);
            //插入数据
            Integer row1 = orderMapper.insertOrderItem(orderItem);
            if(row1 != 1){
                throw new UpdateException("插入数据异常");
            }
        }
        return order;
    }

    @Override
    public Order createNow(Integer aid, Integer uid, Integer pid ,String username) {
        //根据pid获取商品信息
        Product product = productService.findById(pid);
        //获取商品的单价
        Long totalPrice = product.getPrice();
        //根据aid获取收货地址数据
        Address address = addressService.getByAid(aid,uid);
        //补全数据
        Order order = new Order();
        order.setUid(uid);
        //收货地址数据
        order.setRecvName(address.getName());
        order.setRecvPhone(address.getPhone());
        order.setRecvProvince(address.getProvinceName());
        order.setRecvCity(address.getCityName());
        order.setRecvArea(address.getAreaName());
        order.setRecvAddress(address.getAddress());
        //补全支付、总价、时间
        order.setStatus(0);
        order.setTotalPrice(totalPrice);
        order.setOrderTime(new Date());
        //补全日志
        order.setCreatedTime(new Date());
        order.setModifiedTime(new Date());
        order.setCreatedUser(username);
        order.setModifiedUser(username);
        //创建订单数据
        Integer row = orderMapper.insertOrder(order);
        if(row != 1){
            throw new UpdateException("插入数据异常");
        }
        //创建订单详细项数据
        OrderItem orderItem = new OrderItem();
        //补全数据
        orderItem.setOid(order.getOid());
        orderItem.setPid(product.getId());
        orderItem.setTitle(product.getTitle());
        orderItem.setImage(product.getImage());
        orderItem.setPrice(product.getPrice());
        orderItem.setNum(product.getNum());
        //补全日志
        orderItem.setCreatedTime(new Date());
        orderItem.setModifiedTime(new Date());
        orderItem.setCreatedUser(username);
        orderItem.setModifiedUser(username);
        //插入数据
        Integer row1 = orderMapper.insertOrderItem(orderItem);
        if(row1 != 1){
            throw new UpdateException("插入数据异常");
        }
        return order;
    }
}
